package cn.edu.scau.cmi.mazhuanglong.client;

import java.util.Scanner;

import cn.edu.scau.cmi.mazhuanglong.composite.consistent.*;

public class ConsistentCompositeClient {

	public static void addNode(ConsistentOrganization node, String childName, String teamName, Boolean isTeam) {
//		String name;
//		if (isTeam)
//			name = ((ConsistentTeam) node).getName();
//		else
//			name = ((ConsistentPerson) node).getName();
		String name = (node instanceof ConsistentTeam) ? ((ConsistentTeam) node).getName()
				: ((ConsistentPerson) node).getName();

		if (name.equals(teamName)) {
			ConsistentOrganization newNode = isTeam ? new ConsistentTeam(childName) : new ConsistentPerson(childName);
			node.addChild(newNode);
			return;
		}
		for(ConsistentOrganization child : node.getChildren()) {
			addNode(child, childName, teamName, isTeam);
		}
	}

	private static void Play(ConsistentOrganization node, int depth) {
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		if (node instanceof ConsistentPerson) {
			String name = ((ConsistentPerson) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:person");
		} else if (node instanceof ConsistentTeam) {
			String name = ((ConsistentTeam) node).getName();
			System.out.println(name + ":{");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("type:team");
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("children:[");
			for (ConsistentOrganization child : node.getChildren()) {
				Play(child, depth + name.length() + 12);
			}
			for (int i = 0; i < depth + name.length() + 2; i++)
				System.out.print(" ");
			System.out.println("]");
		}
		for (int i = 0; i < depth; i++)
			System.out.print(" ");
		System.out.println("}");
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConsistentOrganization root = new ConsistentTeam("Root");
		System.out.println("根节点：Root");
		String name;
		String team;
		Scanner str = new Scanner(System.in);
		while (true) {
			System.out.println("1、新增个人；2、新增团队；0、运行结束");
			int op = str.nextInt();
			if (op == 0)
				break;
			switch (op) {
			case 1: {
				System.out.println("输入个人名字");
				name = str.next();
				System.out.println("输入此人的团队");
				team = str.next();
				addNode(root, name, team, false);
				break;
			}
			case 2: {
				System.out.println("输入团队名字");
				name = str.next();
				System.out.println("输入团队的团队");
				team = str.next();
				addNode(root, name, team, true);
				break;
			}
			default:
				break;
			}
			Play(root, 0);
		}
	}

}
